#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn=2500000;

ll gen(int m, int n, int a, int md, int v[], int &an) {
  ll ret=0;
  for (int i=1; i<=m; i++) {
    if (a) {
      ret += (v[++an] = a);
    }
    a = (a*58+md)%(n+1);
  }
  return ret;
}

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  static int a[maxn+1], b[maxn+1];
  int m, n, u, md, v, nd, an=0, bn=0;
  ll sa=0, sb;
  scanf("%d %d %d %d %d %d", &m, &n, &u, &md, &v, &nd);
  gen(m, n, u, md, a, an);
  sb = gen(n, m, v, nd, b, bn);
  sort(a+1, a+an+1);
  sort(b+1, b+bn+1);

  ll ans=sb;
  for (int i=1, j=bn; i<=an; i++) {
    sa += a[i];
    for (; j>0 && sb-b[j]-(an-i)*(j-1)<=sb-(an-i)*j; sb-=b[j--]);
    ans = min(ans, (ll)(an-i)*bn+sa+sb-(ll)(an-i)*j);
  }
  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
